package common.socket.tcp;

public class Unicode {
	public static String decode(byte in[], int offset, int length)
	{
		StringBuffer buff = new StringBuffer();
        int max = offset + length;
        char c = 0;
        for (int i = offset; i < max; i++) {   
        	c = 0;
            c |= ((in[i++] & 0xff));
            if(i >= max)
            {
            	c = 0;
            }
            else
            {
            	c |= ((in[i] & 0xff) << 8); 
            }            
            buff.append(c);
        }
        return buff.toString();
	}
	
	public static byte[] encode(String in)
	{
		int i;
		int iLen = in.length();
		byte buff[] = new byte[2*iLen];		
		char ac[] = new char[iLen];		
		in.getChars(0, iLen, ac, 0);
		
		for(i=0; i<iLen; i++)
		{
			buff[2*i] = (byte)((ac[i]&0x00ff));
			buff[2*i+1] = (byte)((ac[i]&0xff00)>>8);
		}
	
		return buff;
	}
}
